package db.migration;

import java.util.List;

import net.jiastudy.biz.targettask.entity.TargetTask;
import net.jiastudy.biz.targettask.service.TargetTaskService;
import net.jiastudy.core.base.BaseMigration;
import net.jiastudy.core.utils.SpringUtility;

public class V1_0_25__add_task_level_name extends BaseMigration {
    private TargetTaskService targetTaskService = SpringUtility.getBean(TargetTaskService.class);
    
    @Override
    public void migrate() throws Exception {
        if (!isFieldExist("target_task", "level_name")) {
            jdbcTemplate.update("ALTER TABLE `target_task` ADD `level_name` varchar(255) COMMENT '等级名称';");
        }
        
        this.dealOldData();
    }

    private void dealOldData() {
        List<TargetTask> tasks = targetTaskService.findAllTargetTask();
        if (null != tasks && tasks.size() > 0) {
            for (TargetTask task : tasks) {
                task.setLevelName(task.getLevelDesc());
                targetTaskService.updateTargetTask(task);
            }
        }
    }
}
